package com.yan;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import com.yan.bean.Link;
import com.yan.mapper.LinkMapper;
import com.yan.service.RedisService;
import com.yan.service.TryableService;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootApplication
public class Application implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer config) {
        // TODO Auto-generated method stub
        config.setPort(8899);
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext appContext = SpringApplication.run(Application.class, args);

        LinkMapper linkMapper = appContext.getBean("linkMapper", LinkMapper.class);

        Link link = linkMapper.find();

        System.out.println(link);

        RedisService redisService = appContext.getBean("redisService", RedisService.class);
        // String
        redisService.set("redisKey", "yan");
        String redisValue = (String) redisService.get("redisKey");
        System.out.println(redisValue);

        // // Set
        redisService.add("setAdd", "liang");
        System.out.println(redisService.setMembers("setAdd"));

        // // Hash Map
        redisService.hmSet("hash", "hashKey", "hashValue");
        System.out.println(redisService.hmGet("hash", "hashKey"));

        redisService.lPush("lpList", "lp");
        System.out.println(redisService.lRange("lpList", 0, 1));

        TryableService tryable = appContext.getBean("tryableService", TryableService.class);
        try {
            tryable.call();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            redisService.setBit("20170-09-01", 10001L, true);
            redisService.setBit("20170-09-01", 10002L, true);
            redisService.setBit("20170-09-01", 10003L, true);
            redisService.setBit("20170-09-04", 10001L, true);
            redisService.setBit("20170-09-05", 10001L, true);
            System.out.println("统计数字=" + redisService.getBit("20170-09-01"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    //    System.out.println(tryable.getCurrentDate());

        RedisTemplate<?, ?> template = (RedisTemplate<?, ?>) appContext.getBean("redisTemplate");

        Map<String, Object> updateLinkMap = new HashMap<String, Object>();
        updateLinkMap.put("orderNo", "0");
        updateLinkMap.put("id", "1");

        redisService.InHashMap("testRedidMap", updateLinkMap);
        Map<Object, Object> testRedisMap = redisService.getHashKey("testRedidMap");

        System.out.println("testRedisMap:" + testRedisMap);

        boolean flag = redisService.isExist("redisKey", "yan");

        System.out.println("flag:" + flag);
        long number = redisService.getHashIncr("order:seed", "orderseed");
        long number1 = redisService.getHashIncr("order:seed", "orderseed");

        System.out.println("number  number1 = " + number + " " + number1);


        template.convertAndSend("chat", "Hello from Redis!");

        template.convertAndSend("chat", "Hello from Redis.....!");
    }

}
